﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineLibrary.ToLinq;
using FinalOnlineLibrary.Models;
using FinalOnlineLibrary.DAL;

namespace FinalOnlineLibrary.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        OnlineLibrary.ToLinq.OnlineLibraryDBDataContext user = new OnlineLibrary.ToLinq.OnlineLibraryDBDataContext();
        
        public ActionResult Index()
        {

            if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
            {
                IEnumerable<OnlineLibrary.ToLinq.tblUser> list = user.tblUsers.ToList();
                return View(list);
            }
            else
            {
                return Redirect("Shared/Error");
            }
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            OnlineLibrary.ToLinq.tblUser UserView = user.tblUsers.Single(p => p.UserId == id);
            ViewData["UserName"] = UserView.Name;
            return View(UserView);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]

        public ActionResult Create(OnlineLibrary.ToLinq.tblUser collection)
        {
            try
            {
                // TODO: Add insert logic here
                if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true) || (Boolean.Parse(Session["Guest"].ToString()) == true))
                {
                    var exist_user = from e_user in user.tblUsers where e_user.UserName == collection.UserName select e_user;
                    if (exist_user.Count() != 0)
                    {
                        ViewData["error"] = "Username has been existed";
                        return View();
                    }
                    else if (collection.PassWord.ToString().Length < 5)
                    {
                        ViewData["error"] = "Password is too short";
                        return View();
                    }
                    collection.Role = 7;
                    user.tblUsers.InsertOnSubmit(collection);
                    user.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect("Shared/Error");
                }
            }
            catch
            {
                return View();
            }
        }


        //
        //Get:/User/Changepass/5
        public ActionResult Changepass(int id)
        {
            if ((Boolean.Parse(Session["Login"].ToString()) == true))
            {
                OnlineLibrary.ToLinq.tblUser UserView = user.tblUsers.Single(p => p.UserId == id);
                return View(UserView);
            }
            else {
                return RedirectToAction("Index");
            }
            
        }
        [HttpPost]
        public ActionResult Changepass(int id,ChangePasswordModel model)
        {
            try 
            {
                using (OnlineLibraryDBDataContext onlineContext = new OnlineLibraryDBDataContext())
                {
                    var list = onlineContext.tblUsers.SingleOrDefault(p => p.UserId == id);

                    if (list.PassWord == model.OldPassword)
                    {
                        if ((Boolean.Parse(Session["Login"].ToString()) == true))
                        {
                            if (model.NewPassword == model.ConfirmPassword)
                            {
                                list.PassWord = model.NewPassword;
                                onlineContext.SubmitChanges();
                                return RedirectToAction("ChangePasswordSuccess");
                            }
                            else {
                                ViewData["error"] = "Wrong new password. Retype";
                                return View();
                            }
                        }
                        else
                        {
                            ViewData["error"] = "Log in, please";
                            return View();
                        }
                    }
                    else {
                        ViewData["error"] = "Wrong password. Retype";
                        return View();
                    }
                    
                }

            }
            catch
            {
                return View();
            }

        }
        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id)
        {
            OnlineLibrary.ToLinq.tblUser UserView = user.tblUsers.Single(p => p.UserId == id);
            ViewData["DDLRoles"] = new SelectList(DALBooks.GetAllRoles().ToList(), "RoleId", "RoleName",UserView.Role);

            return View(UserView);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, OnlineLibrary.ToLinq.tblUser collection, FormCollection form)
        {
            try
            {
                using (OnlineLibraryDBDataContext onlineContext = new OnlineLibraryDBDataContext())
                {
                    // TODO: Add update logic here
                    var list = onlineContext.tblUsers.SingleOrDefault(p => p.UserId == id);
                    list.UserId = collection.UserId;
                    list.Name = collection.Name;
                    list.UserName = collection.UserName;
                    //list.PassWord = collection.PassWord;
                    list.Address = collection.Address;
                    if ((Boolean.Parse(Session["Admin"].ToString()) == true))
                    {
                        list.Role = int.Parse(form["DDLRoles"]);
                        onlineContext.SubmitChanges();
                    }
                    else
                    { onlineContext.SubmitChanges(); }

                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id)
        {
            if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
            {
                OnlineLibrary.ToLinq.tblUser UserView = user.tblUsers.Single(p => p.UserId == id);
                if (UserView.Available == true)
                {
                    ViewData["Warning"] = "Are yousure you want to delete this user " + UserView.UserName + " ?";
                }
                else
                {
                    ViewData["Warning"] = "You cann't delete this user";
                }
                return View(UserView);
            }
            else
            {
                return Redirect("Shared/Error");
            }
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
                {
                    // TODO: Add delete logic here
                    var list = user.tblUsers.Single(p => p.UserId == id);
                    user.tblUsers.DeleteOnSubmit(list);
                    user.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect("Shared/Error");
                }
            }
            catch
            {
                return View();
            }
        }
    }
}
